﻿using Microsoft.EntityFrameworkCore;
using System;

namespace FwProject.Comm
{

    public class SqlFunction
    {

        /// <summary>
        /// Counts the number of day boundaries crossed between the startDate and endDate.
        /// Corresponds to SQL Server's DATEDIFF(DAY,startDate,endDate).
        /// </summary>
        /// <param name="startDate">Starting date for the calculation.</param>
        /// <param name="endDate">Ending date for the calculation.</param>
        /// <returns>Number of day boundaries crossed between the dates.</returns>
        public static int DateDiffDay(DateTime startDate, DateTime endDate)
        {
            TimeSpan diff = endDate.Date - startDate.Date;
            return diff.Days;
        }

        /// <summary>
        /// Counts the number of day boundaries crossed between the startDate and endDate.
        /// Corresponds to SQL Server's DATEDIFF(DAY,startDate,endDate).
        /// </summary>
        /// <param name="startDate">Starting date for the calculation.</param>
        /// <param name="endDate">Ending date for the calculation.</param>
        /// <returns>Number of day boundaries crossed between the dates.</returns>
        public static int? DateDiffDay(DateTime? startDate, DateTime? endDate)
        {
            if (startDate.HasValue && endDate.HasValue)
            {
                return DateDiffDay(startDate.Value, endDate.Value);
            }
            else
            {
                return null;
            }
        }
        [DbFunction]
        public static DateTime DateFunction(int Year, int Month, int Day)
        {
            return new DateTime(Year, Month, Day);
        }

    }
}
